#include "ConnectionDialog.h"

ConnectionDialog::ConnectionDialog( const QSqlDatabase& database, QWidget* parent, Qt::WindowFlags flags )
    : QDialog( parent, flags )
	, mDatabase( database )
{
	QFormLayout* layout = new QFormLayout();
	setLayout( layout );

	// Host
	QLabel* hostTitle = new QLabel( tr( "Host :" ), this );
	mHostInput = new QLineEdit( tr( "localhost" ), this );
	layout->addRow( hostTitle, mHostInput );

	// Port
	QLabel* portTitle = new QLabel( tr( "Port :" ), this );
	mPortInput = new QLineEdit( "3307", this );
	mPortInput->setInputMask( tr( "0000" ) );
	layout->addRow( portTitle, mPortInput );

	// Username
	QLabel* userTitle = new QLabel( tr( "User Name :" ), this );
	mUserInput = new QLineEdit( tr( "root" ), this );
	layout->addRow( userTitle, mUserInput );

	// Password
	QLabel* passTitle = new QLabel( tr( "Password :" ), this );
	mPassInput = new QLineEdit( tr( "password" ), this );
	mPassInput->setEchoMode( QLineEdit::Password );
	layout->addRow( passTitle, mPassInput );

	// Connect
	QPushButton* connectButton = new QPushButton( tr( "Connect" ), this );
	connect( connectButton, SIGNAL( pressed() ), this, SLOT( connectDatabase() ) );
	QPushButton* cancelButton = new QPushButton( tr( "Cancel" ), this );
	connect( cancelButton, SIGNAL( pressed() ), this, SLOT( cancel() ) );
	layout->addRow( connectButton, cancelButton );
}

ConnectionDialog::~ConnectionDialog()
{
}

void
ConnectionDialog::connectDatabase()
{
	done( 0 );
}

void 
ConnectionDialog::cancel()
{
	mDatabase.setHostName( mHostInput->text() );
	mDatabase.setDatabaseName( mPortInput->text() );
    mDatabase.setUserName( mUserInput->text() );
    mDatabase.setPassword( mPassInput->text() );
	done( 0 );
}